/**
 * Created by yanguoxu on 2017/4/10.
 * 业务相关的公共函数
 */
import request from "@/util/request"
import api from "@/util/api"
let publicFunc
publicFunc = {
    tidyCourseList(courseAry){
        var cur_time = "";
        var newAry = [];
        var time_Ary = [];
        for(var i=0;i<courseAry.length;i++){
            var item = courseAry[i];
            var time = item["timestamp"];
            var hour = Math.floor(time/(1000*60*60))*1000*60*60;
            if (cur_time==hour){
                time_Ary["time_ary"].push(item);
            }else{
                cur_time = hour;
                time_Ary = {
                    "time":hour,
                    "time_ary":[item]
                }
                newAry.push(time_Ary);
            }

        }
        //console.dir(newAry);
        return newAry;
    },
    isEmail(str){
        var reg = /^([a-zA-Z0-9_-].?[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//学堂官网版本
        return reg.test(str);
    },

    setPageTitleSchoolName(second_title){
        var schoolName = $(".page-header .school-name").val();
        schoolName = $.trim(schoolName);
        if(schoolName!==""){
            window.document.title ="" + schoolName +second_title+"-雨课堂Pro";
        }else{
            setTimeout(function(){
                var schoolName = $(".page-header .school-name").val();
                schoolName = $.trim(schoolName);
                if (schoolName!=""){
                    window.document.title ="" + schoolName +second_title+"-雨课堂Pro";
                }

            },3000);
        }

    },
    toFixed(value,n) {
        var v = Number(value)
        var n = n || 0
        if(v){
            var l = Math.pow(10,n)
            var num = Math.round(v * l)
            var f = (num / l).toFixed(n)
            f == parseInt(f) && (f = parseInt(f))
            return f
        } else {
            return value
        }
    },
    getUserInfo(suc, err) {
        request.get(api.header).then(function (e) {
            suc && suc(e)
        }).catch(function (e) {
            err && err(e)
        })
    }
}

export default publicFunc
